package com.sk.controller;


import com.sk.entity.Users;
import com.sk.service.IUsersService;
import com.sk.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author smith
 * @since 2025-05-27
 */
@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private IUsersService usersService;

    @GetMapping("{userId}")
    public Result getById(@PathVariable("userId") Integer userId){
        return usersService.getById(userId);
    }

    // 注册
    @PostMapping
    public Result save(@RequestBody Users user){
        System.out.println("Received user: " + user);
        user.setCreateTime(LocalDateTime.now());
        user.setUserId(null);  // 主键自增，避免0
        user.setUserAvatar("images/default.png");
        user.setStatus(0);
        return usersService.saveuser(user);
    }

    @PostMapping("/login")
    public Result login(@RequestParam String name, @RequestParam String password) {
        return usersService.login(name, password);
    }



}
